package com.icode.mvc.view;

import com.icode.mvc.exception.ViewException;
import com.icode.mvc.model.Model;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;
import java.util.Set;

/**
 * User: Alex Zhong
 * Date: 2016/2/4
 */
public class ForwardView implements View {
    private String path;

    @Override
    public void render(HttpServletRequest request, HttpServletResponse response, Model model) throws ViewException {
        try {
            Set<Map.Entry<String, Object>> entries = model.entrySet();
            for (Map.Entry<String, Object> entry : entries) {
                String key = entry.getKey();
                Object value = entry.getValue();
                request.setAttribute(key, value);
            }

            RequestDispatcher dispatcher = request.getRequestDispatcher(path);
            dispatcher.forward(request, response);
        } catch (Exception e) {
            throw new ViewException(e);
        }
    }
}
